<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<style>
    tr,
    th,
    td {
        padding: 20px 30px;
    }
</style>

<body>

    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <div class="box">
        <input type="text" v-model="news.name" placeholder="新闻名称">
        <input type="text" v-model="news.link" placeholder="新闻链接">
        <input type="text" v-model="news.description" placeholder="新闻描述">
        <button @click="add()">添加</button><br><br>

        <table border="1">
            <thead>
                <tr>
                    <th>id</th>
                    <th>名称</th>
                    <th>链接</th>
                    <th>描述</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody>

                <tr v-for="(item,index) in arr" :key="item.id" v-color="index">
                    <td>{{index+1}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.link}}</td>
                    <td>{{item.description}}</td>
                    <td><button @click="del(index)">删除</button></td>
                </tr>

                <!-- <tr v-for="(item,index) in arr" :key="item.id" :style="{backgroundColor:index%2==0?'red':'yellow'}">
                    <td>{{index+1}}</td>
                    <td>{{item.name}}</td>
                    <td>{{item.link}}</td>
                    <td>{{item.description}}</td>
                    <td><button @click="del(index)">删除</button></td>
                </tr> -->

            </tbody>
        </table>
    </div>

    <script>
        Vue.directive('color', function (ele, binding) {
            if (binding.value % 2 == 0) {
                ele.style.backgroundColor = 'aqua'
            } else {
                ele.style.backgroundColor = 'pink'
            }
        })

        new Vue({
            el: '.box',
            data: {
                news: {
                    name: '',
                    link: '',
                    description: '',
                },
                arr: [{
                    id: 1,
                    name: '哔哩哔哩',
                    link: 'https://www.bilibili.com/',
                    description: '好看',
                }]
            },
            methods: {
                add() {
                    this.arr.push({ ...this.news })
                    this.news.name = ''
                    this.news.link = ''
                    this.news.description = ''
                },
                del(index) {
                    this.arr.splice(index, 1)
                },

            }
        })
    </script>

</body>

</html>